<?php

require_once BaseUtil::getModulePath ( "base" ) . '/models/BaseDao.php';

class OrderItemsDao extends BaseDao {
	
    protected $_name;

    public function _setup() {
    	$this->_name=$this->prefix() . "order_items";
    	parent::_setup();
    }
    
    public function getItemsById($orderId) {
        $id = $orderId;
    	$select=$this->db_r->select();
        $select->from($this->_name,array('id as oiid','item_id','status','quantity','sale_price','deal_price'));
        $select->join('product_items','order_items.item_id=product_items.id','*');
        $select->join('products','product_items.product_id=products.id',array('title', 'url_title'));
        $select->where("order_items.is_delete=0");
        $select->where('order_items.order_id=?',$id);
        $sql = $select->__toString();
        $value=$this->db_r->fetchAll($sql);
        return $value;
    }
    
    public function getItemById($id){
    	$select = $this->db_r->select();
    	$select->from($this->_name, array('id as oiid','item_id','status','quantity','sale_price','deal_price'));
    	$select->join('product_items','order_items.item_id=product_items.id','*');
    	$select->join('products','product_items.product_id=products.id','title');
    	$select->where("order_items.is_delete=0");
    	$select->where('order_items.id =?', $id);
        $sql = $select->__toString();
        $item=$this->db_r->fetchRow($sql);
        return $item;
    }

    public function getPackageItemsById($id,$orderid) {
        $ids=explode(',',$id);
        $select=$this->db_r->select();
        $select->from($this->_name,'*');
        $select->join('product_items','order_items.item_id=product_items.id','*');
        $select->join('products','product_items.product_id=products.id','title');
        $select->where("order_items.is_delete=0");
        $select->where('order_items.order_id=?',$orderid);
        $select->where('order_items.item_id in(?)',array($ids));
        $sql=$select->__toString();
        $value=$this->db_r->fetchAll($sql);
        /*print_r($value);
        exit();*/
        return $value;
    }
    
    public function getPackageByPlace($id) {
        $select=$this->db_r->select();
        $select->from($this->_name,'*');
        $select->join('product_items','order_items.item_id=product_items.id','*');
        $select->join('products','product_items.product_id=products.id','title');
        $select->where("order_items.is_delete=0");
        $select->where('order_items.order_id=?',$id);
        $sql=$select->__toString();
        $value=$this->db_r->fetchAll($sql);
        return $value;
    }    
    
    public function addOrderItem($orderitem){
    	return $this->insertData($orderitem);
    } 
    
    public function cancelItem($id, $cancel){
    	$where = $this->db_w->quoteInto('id=?', $id);
    	$this->updateData($cancel, $where);
    }
    
    public function getPartItemById($id){
    	$select = $this->db_r->select();
    	$select->from($this->_name, array('order_id', 'item_id'));
    	$select->where("is_delete=0");
    	$select->where('id=?', $id);
    	$sql = $select->__toString();
    	$item = $this->db_r->fetchRow($sql);
    	return $item;
    }
    
    public function delItemsById($id){
    	$isDel = array("is_delete"=>1);
    	$where = $this->db_w->quoteInto('order_id=?', $id);
    	$this->updateData($isDel, $where);
    }  
    
    public function getItemsByOrderId($orderId){
    	$select = $this->db_r->select();
    	$select->from($this->_name, "*");
    	$select->where("is_delete=0");
        $select->where('order_id=?', $orderId);
        $sql = $select->__toString(); 
        return $this->db_r->fetchAll($sql);
    }
}